package com.LeeCode;

import java.util.HashMap;
import java.util.Map;

/**
 * 识别数组中的最大异常值
 */

public class Code3371 {
    public static void main(String[] args) {
        int[] nums = {6, -31, 50, -35, 41, 37, -42, 13};
        System.out.println(new Code3371().getLargestOutlier(nums));

    }

    public int getLargestOutlier(int[] nums) {
        Map<Integer, Integer> map = new HashMap<>();
        int totalSum = 0;
        for (int num : nums) {
            totalSum += num;
            map.merge(num, 1, Integer::sum);
        }

        int ans = Integer.MIN_VALUE;
        for (int num : map.keySet()) {
            int target = totalSum - num * 2;
            if (map.containsKey(target)) {
                if (target != num || map.get(target) > 1)
                    ans = Math.max(ans, target);
            }
        }
        return ans;
    }
}
